﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Data;

namespace Admin.cadastros
{
    public partial class tipoproduto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String script =  @"$(document).ready(function () 
                                {  
                                    $('#btnGravar').button();
                                });";
            ScriptManager.RegisterClientScriptBlock(btnGravar, btnGravar.GetType(), "botao",script, true);
            if(!IsPostBack)
                getData();
        }

        protected void btnGravar_Click(object sender, EventArgs e)
        {
            //Creating the first row of GridView to be Editable
            LojaAdminDataContext dc = new LojaAdminDataContext();
            List<TipPro> dt = new List<TipPro>();
            dt.Add(new TipPro());
            dt.AddRange((from tipo in dc.GetTable<TipPro>()
                      select tipo).ToList());
            GridView1.EditIndex = 0;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            //Changing the Text for Inserting a New Record
            ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Inserir";
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {            
            Table<TipPro> tipo = new LojaAdminDataContext().GetTable<TipPro>();
            TipPro tippro = new TipPro();
            int codigo = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
            tippro.TppID = (short)(codigo);
            tippro.TppDes = GridView1.Rows[e.RowIndex].Cells[2].Text;
            try
            {
                tipo.DeleteOnSubmit(tippro);
                tipo.Context.SubmitChanges();
                getData();
            }
            catch (Exception error)
            {                
                throw new Exception(error.Message);
            }
            
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Table<TipPro> tipo = new LojaAdminDataContext().GetTable<TipPro>();            
            TipPro tippro = new TipPro();
            int codigo = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
            tippro = tipo.Where(t => t.TppID == (short)(codigo)).SingleOrDefault();
            if (tippro != null)
            {
                tippro.TppDes = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                tipo.Context.SubmitChanges();
                GridView1.EditIndex = -1;
            }
            else {
                tippro = new TipPro();
                tippro.TppID = (short)(GridView1.Rows.Count);
                tippro.TppDes = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                tipo.InsertOnSubmit(tippro);
                tipo.Context.SubmitChanges();
                GridView1.EditIndex = -1;
            }
            getData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            getData();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            getData();
        }

        protected void getData()
        {
            LojaAdminDataContext dc = new LojaAdminDataContext();
            GridView1.DataSource = dc.GetTable<TipPro>();
            GridView1.DataBind();
        }
        
    }
}